package cn.dnf.pet.controller;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.LoginContext;

import cn.dnf.basic.util.PageList;
import cn.dnf.pet.domain.Pet;
import cn.dnf.pet.domain.SearchMasterMsg;
import cn.dnf.pet.dto.AcceptDto;
import cn.dnf.pet.query.PetQuery;
import cn.dnf.pet.query.SearchMasterMsgQuery;
import cn.dnf.pet.service.ISearchMasterMsgService;
import cn.dnf.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/searchMasterMsg")
@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService SearchMasterMsgService;

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    /**
     * http://localhost:8080/SearchMasterMsg  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有宠物信息" )
    public List<SearchMasterMsg> findAll(HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return SearchMasterMsgService.loadAll();
    }

    /**
     * 查询一个  接收参数id
     *   url: http://localhost:8080/SearchMasterMsg/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public SearchMasterMsg findOne(@PathVariable("id") Long id){
        return SearchMasterMsgService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){

        try {
            SearchMasterMsgService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/SearchMasterMsg
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updateOrAdd(@RequestBody SearchMasterMsg searchMasterMsg){

        try {
            if(searchMasterMsg.getId()==null){
                // 添加操作
                SearchMasterMsgService.save(searchMasterMsg);
            }else{
                // 修改操作
                SearchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "寻主" )
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg smm,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService.publish(smm,logininfo.getId());
            return JsonResult.me().setMsg("寻主成功");
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常");
        }
    }


    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            SearchMasterMsgService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * 待处理
     * @return
     */
    @ApiOperation(value = "待处理查询接口" )
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.toHandle(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 用户查询寻主信息
     * @return
     */
    @ApiOperation(value = "用户" )
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return searchMasterMsgService.user(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 拒单
     * @return
     */
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "拒单" )
    public JsonResult reject(@PathVariable("id") Long id){

        try {
            SearchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    @PostMapping("/accept")
    @ApiOperation(value = "接单" )
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request){

        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            SearchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }


    }

}
